package com.jinxin.platform.cdfaceacs.support;

import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdfaceacs.pojo.domains.CdattendancerecordRecord;
import com.jinxin.platform.cdfaceacs.pojo.domains.CdattendancerecordReport;
import com.jinxin.platform.cdfaceacs.pojo.domains.CdattendancerecordUser;
import com.jinxin.platform.cdfaceacs.service.CdattendancerecordRecordService;
import com.jinxin.platform.cdfaceacs.service.CdattendancerecordReportService;
import com.jinxin.platform.cdfaceacs.service.CdattendancerecordUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Date: 2021-04-23
 * Author: yangjie
 * desc:
 **/
@Slf4j
@Service
public class AsyncSupport {
    @Resource
    CdattendancerecordUserService userService;
    @Resource
    CdattendancerecordReportService reportService;
    @Resource
    CdattendancerecordRecordService recordService;

    @Async
    public void dealData(JSONObject reportMessage, String serial_num) {
        String workNo = null;
        String name = null;
        String time = null;
        log.info("考勤记录模块上报数据,reportMessage,{}", reportMessage);
        String regex = "[||]+";
        String r2Result = reportMessage.getString("r2");
        if(StringUtils.isEmpty(r2Result) || (!(r2Result.contains("dwMajor") && r2Result.contains("dwMinor")))){
            return;
        }
        String[] split = r2Result.split(regex);
        for (String content : split) {
            //dwMajor=5且dwMinor=75才是正常的数据
            if(content.contains("dwMajor")){
                String dwMajor = content.substring(content.indexOf("=") + 1);
                if(StringUtils.isEmpty(dwMajor) || (!dwMajor.equals("5"))){
                    return;
                }
            }
            if(content.contains("dwMinor")){
                String dwMinor = content.substring(content.indexOf("=") + 1);
                if(StringUtils.isEmpty(dwMinor) || (!dwMinor.equals("75"))){
                    return;
                }
            }
            if (content.contains("struEventInfoEx.byEmployeeNo")) {
                System.out.println(content);
                workNo = content.substring(content.indexOf("=") + 1);
            }
            if (StringUtils.isEmpty(workNo) && content.contains("struAcsEventInfo.dwEmployeeNo")) {
                workNo = content.substring(content.indexOf("=") + 1);
            }
            if (content.contains("sNetUser")) {
                System.out.println(content);
                name = content.substring(content.indexOf("=") + 1);
            }
            if (content.contains("struTime")) {
                System.out.println(content);
                time = content.substring(content.indexOf("=") + 1);
            }
        }
        //todo
        log.info("考勤记录模块,接受到数据，插入数据库之前记录一下,{},{},{},{}", workNo, name, serial_num, time);
        insert2Db(workNo, name, serial_num, time);
    }

    public static void main(String[] args) throws ParseException {
        String time = "2020/12/30 13:31:9";
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date parse = formatter.parse(time);
        System.out.println(parse);
    }

    /**
     * 插入上报信息到数据库
     *
     * @param workNo 工号
     * @param name   人员名称
     */
    private void insert2Db(String workNo, String name, String serial_num, String time) {
        if (StringUtils.isEmpty(workNo)) {
            return;
        }
        if (StringUtils.isEmpty(time)) {
            return;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date nowDate = null;
        try {
            nowDate = formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //更新用户信息
        updateUser(workNo, name, nowDate);
        //插入上报数据
        insertReport(workNo, name, nowDate, serial_num);
        //更新综合数据
        insertRecord(workNo, name, nowDate, serial_num);
    }

    private void insertRecord(String workNo, String name, Date nowDate, String serial_num) {
        CdattendancerecordUser user = userService.getUserByWorkNo(workNo);
        CdattendancerecordRecord resultRecord = recordService.getByWorkNoAndDate(workNo, nowDate);
        if (resultRecord == null) {
            CdattendancerecordRecord insert = new CdattendancerecordRecord();
            insert.setDevMac(serial_num);
            insert.setRecordDate(nowDate);
            if (StringUtils.isEmpty(name)) {
                insert.setUserName(user.getUserName());
            } else {
                insert.setUserName(name);
            }
            insert.setWorkNo(user.getWorkNo());
            insert.setOrgName(user.getOrgName());
            insert.setFirstDate(nowDate);
            recordService.insert(insert);
        } else {
            if (StringUtils.isEmpty(name)) {
                resultRecord.setUserName(user.getUserName());
            } else {
                resultRecord.setUserName(name);
            }
            resultRecord.setWorkNo(user.getWorkNo());
            resultRecord.setOrgName(user.getOrgName());
            resultRecord.setLastDate(nowDate);
            recordService.update(resultRecord);
        }
    }

    private void insertReport(String workNo, String name, Date nowDate, String serial_num) {
        CdattendancerecordReport report = new CdattendancerecordReport();
        report.setWorkNo(workNo);
        report.setUserName(name);
        report.setReportDate(nowDate);
        report.setDevMac(serial_num);
        reportService.insert(report);
    }

    /**
     * 更新用户信息
     */
    private void updateUser(String workNo, String name, Date nowDate) {
        CdattendancerecordUser user = userService.getUserByWorkNo(workNo);
        if (user != null) {
            if (!StringUtils.isEmpty(name)) {
                user.setUserName(name);
            }
            user.setLastRecordDate(nowDate);
            userService.update(user);
        } else {
            CdattendancerecordUser insert = new CdattendancerecordUser();
            insert.setLastRecordDate(nowDate);
            insert.setUserName(name);
            insert.setWorkNo(workNo);
            userService.insert(insert);
        }
    }

}
